package com.aistar.serviceVO.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.mapper.DynamicCollectMapper;
import com.aistar.mapper.DynamicMapper;
import com.aistar.pojo.*;
import com.aistar.pojoVO.DynamicAndCollectVO;
import com.aistar.serviceVO.DynamicAndCollectVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DynamicAndCollectVOServiceImpl implements DynamicAndCollectVOService {
    private final DynamicCollectMapper dynamicCollectMapper;
    private final DynamicMapper dynamicMapper;
    private final CustomerMapper customerMapper;

    @Autowired
    public DynamicAndCollectVOServiceImpl(
            DynamicCollectMapper dynamicCollectMapper,
            DynamicMapper dynamicMapper,
            CustomerMapper customerMapper) {
        this.dynamicCollectMapper = dynamicCollectMapper;
        this.dynamicMapper = dynamicMapper;
        this.customerMapper = customerMapper;
    }


    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        DynamicCollectExample example = new DynamicCollectExample();
        example.createCriteria()
                .andCustomerIdEqualTo(customerId)
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        example.setOrderByClause("dynamic_collect_time desc");
        List<DynamicCollect> dynamicCollectList = dynamicCollectMapper.selectByExample(example);
        if (dynamicCollectList != null && dynamicCollectList.size() > 0) {
            List<DynamicAndCollectVO> dynamicAndCollectVOList = new ArrayList<>();
            for (DynamicCollect dynamicCollect : dynamicCollectList) {
                Dynamic dynamic = dynamicMapper.selectByPrimaryKey(dynamicCollect.getDynamicId());
                Customer customer = customerMapper.selectByPrimaryKey(dynamicCollect.getCustomerId());
                dynamicAndCollectVOList.add(new DynamicAndCollectVO(dynamicCollect, dynamic, customer));
            }
            return ServerResponse.getSuccess(dynamicAndCollectVOList);
        }
        return ServerResponse.getFailed();
    }
}
